package org.flower.dao.impl;

import org.flower.bean.CommodityDetails;
import org.flower.bean.Order;
import org.flower.bean.OrderDetails;
import org.flower.bean.OrderState;
import org.flower.dao.OrderDao;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public List<Order> getById(int id ,String  zt) {
        List<Order> list = new ArrayList<>();
        String sql="select * from `order` where userId=? ";
        if(!zt.equals("0")&&zt.length()<2){
            sql=sql+"and orderState="+zt;
        }
        if(zt.length()>2){
            sql+="and id="+zt;
        }
        ResultSet rs =executeQuery(sql,id);
        try {
            while (rs.next()){
                //封装订单
                Order  order = new Order(
                        rs.getString("id"),
                        rs.getString("shouName"),
                        rs.getString("shouPhone"),
                        rs.getString("shouAddress"),
                        rs.getString("dingName"),
                        rs.getString("dingPhone"),
                        rs.getDate("ServiceDate"),
                        rs.getString("GreetingCardMsg"),
                        rs.getString("Content"),
                        rs.getString("couId"),
                        rs.getDouble("copewithPrice"),
                        rs.getDouble("PaidinPrice"),
                        rs.getInt("orderState"),
                        rs.getInt("userId"),
                        rs.getDate("orderDate"));
                list.add(order);
            }
            //循环封装订单详情和商品信息 添加到订单里
            for (Order order:list) {
                List<OrderDetails> orderDetailsList = new ArrayList<>();
                String sql1 = "select cd.comImage,cd.outPrice,od.num,od.totalPrice,cd.comName from `orderdetails` od inner join commoditydetails cd on od.`ComdetailsId`=cd.`id` where orderId = "+order.getId();
                ResultSet rs1 = executeQuery(sql1);
                try {
                    while(rs1.next()){
                        OrderDetails orderDetails = new OrderDetails();
                        orderDetails.setTotalPrice(rs1.getDouble(4));
                        //获取订单详情里的价格，封装到order属性里
                        double sum = orderDetails.getTotalPrice();
                        order.setSumPrice(sum);

                        orderDetails.setNum(rs1.getInt(3));
                        int  num=orderDetails.getNum();
                        order.setSumNum(num);

                        CommodityDetails commodityDetails = new CommodityDetails();
                        commodityDetails.setComname(rs1.getString(5));
                        commodityDetails.setOutprice(rs1.getDouble(2));
                        commodityDetails.setComimage(rs1.getString(1).split(",")[0]);
                        orderDetails.setCommodityDetails(commodityDetails);
                        orderDetailsList.add(orderDetails);
                    }
                    order.setOrderDetailsList(orderDetailsList);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    try {
                        closeAll(rs1,rs1.getStatement(),rs1.getStatement().getConnection());
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return list;
    }

    @Override
    public int getNum(int id,int zt) {
        int num=0;
        String sql="select COUNT(1) from `order` where userId=? and orderstate!=7";
        if(zt!=0){
            sql=sql+" and orderState="+zt;
        }
        ResultSet rs =executeQuery(sql,id);
        try {
            while (rs.next()){
            num=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return num;
    }





    @Override
    public int findDayAddOrder() {
        int result = 0;
        String sql = "SELECT count(1) c FROM `order` WHERE TO_DAYS(orderDate) = TO_DAYS(NOW())";
        ResultSet rs = executeQuery(sql);
        try {
            if(rs.next()){
                result = rs.getInt("c");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int findSumOrder() {
        int result = 0;
        String sql = "SELECT COUNT(1) c FROM `order` ";
        ResultSet rs = executeQuery(sql);
        try {
            if(rs.next()){
                result=  rs.getInt("c");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public boolean updateOrder(Order order) {
        String sql = "UPDATE `order` SET ";
        if(order.getGreetingcardmsg()!=null){
            sql += " GreetingCardMsg="+"'"+order.getGreetingcardmsg()+"' ";
        }
        if(order.getContent()!=null){
            sql += " ,Content="+"'"+order.getContent()+"'";
        }
        if(order.getOrderstate()!=null){
            sql += " ,orderstate="+order.getOrderstate();
        }
        if(order.getServicedateStr()!=null){
            sql += " ,serviceDate="+"'"+order.getServicedateStr()+"'";
        }

        sql += " where id = "+order.getId();
        return executeUpdate(sql);
    }

    @Override
    public List<Order> getAllById(int id) {
        List<Order> list = new ArrayList<>();
        String sql="select * from `order` where userId=?";
        ResultSet rs =executeQuery(sql,id);
        try {
            while (rs.next()){
                Order  order = new Order(
                        rs.getString("id"),
                        rs.getString("shouName"),
                        rs.getString("shouPhone"),
                        rs.getString("shouAddress"),
                        rs.getString("dingName"),
                        rs.getString("dingPhone"),
                        rs.getDate("ServiceDate"),
                        rs.getString("GreetingCardMsg"),
                        rs.getString("Content"),
                        rs.getString("couId"),
                        rs.getDouble("copewithPrice"),
                        rs.getDouble("PaidinPrice"),
                        rs.getInt("orderState"),
                        rs.getInt("userId"),
                        rs.getDate("orderDate"));
                list.add(order);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public boolean DeleteDing(String id) {
        String sql ="update `order` set orderState=7 where id=?";
        return executeUpdate(sql,id);
    }

    @Override
    public boolean updateDing(String id) {
        String sql="update `order` set orderState=5 where id=?";
        return executeUpdate(sql,id);
    }

    @Override
    public boolean fahuo(String orderid) {
        String sql = "UPDATE `order` SET orderState = 3 WHERE id = ?";
        return executeUpdate(sql,orderid);
    }

    @Override
    public boolean addOrder(org.flower.entity.Order order) {
        String sql = "insert into `order` values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,now())";
        Object[] objs = new Object[14];
        objs[0] = order.getId();
        objs[1] = order.getShouName();
        objs[2] = order.getShouPhone();
        objs[3] = order.getShouAddress();
        objs[4] = order.getDingName();
        objs[5] = order.getDingPhone();
        objs[6] = order.getServiceDate();
        objs[7] = order.getGreetingCardMsg();
        objs[8] = order.getContent();
        objs[9] = order.getCouId();
        objs[10] = order.getCopewithPrice();
        objs[11] = order.getPaidinPrice();
        objs[12] = order.getOrderState();
        objs[13] = order.getUserId();
        //执行添加
        return super.executeUpdate(sql,objs);
    }

    @Override
    public boolean updateOrderStateById(String orderId,int state) {
        String sql = "update `order` set orderState=? where id =?";
        return super.executeUpdate(sql,state,orderId);
    }

    @Override
    public boolean updateOrderStaById(String id) {
        String sql="update `order` set orderState=4 where id=?";
        return executeUpdate(sql,id);
    }

    @Override
    public List<Integer> getZhouOrder() {
        List<Integer> list = new ArrayList<>();
        String sql = "SELECT COUNT(1) c FROM `order` WHERE TO_DAYS(orderDate) = TO_DAYS(SUBDATE(NOW(), ?))";
        for (int i = 0; i <= 7; i++) {
            ResultSet rs = executeQuery(sql, i);
            try {
                if(rs.next()){
                    list.add(rs.getInt("c"));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }


}
